package mn.asri.teaproc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/** the main GUI for this POC. */
public class Teaproc implements EntryPoint {

	private TextArea textArea = new TextArea();
	private TextBox textBox = new TextBox();
	private Button button = new Button("next");
	private int currentId;

	public void onModuleLoad() {
		// first set the app as "invisible", it will be set to visible after
		// the first question is fetched.
		final RootPanel main = RootPanel.get("app");
		main.setVisible(false);

		final PocRpcServiceAsync service = PocRpcService.App.getInstance();
		service.receiveInitialQuestion(new AsyncCallback() {

			public void onFailure(Throwable caught) {
				main.clear();
				main.add(new Label("ERROR: " + caught.getMessage()));
				main.setVisible(true);
			}

			public void onSuccess(Object result) {
				PocPayload payload = (PocPayload) result;
				PocQuestion question = new PocQuestion();
				ClientModelUtil.toModel(payload, question);
				currentId = question.getIndex();
				// todo: we need to extract just the question.getText() for proper testing
				String data = "Question\nIndex:\t" +
				              currentId +
				              "\ntype:\t" +
				              question.getType() +
				              "\ntext:\t" +
				              question.getText() +
				              "\nserverTicks:\t" +
				              question.getServerTicks() +
				              "\nresponses:\t" +
				              convert(question.getResponses()) +
				              "\nreferenced:\t" +
				              question.getReferencedObject();
				textArea.setText(data);
				textBox.setText("");

				// now that we got the question, let's show it.
				main.setVisible(true);
			}
		});

		button.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				PocAnswer answer = new PocAnswer();
				answer.setAnswerIndex(Integer.parseInt(textBox.getText()));
				answer.setCurrentTime(System.currentTimeMillis());
				answer.setIndex(currentId);
				answer.setNextQuestionIndex(currentId + 1);
				// dummy
				answer.setTestId("SAMPLE");

				// hide the app while sending answer and getting question from
				// server
				main.setVisible(false);

				service.sendAnswerAndReceiveQuestion(
					ClientModelUtil.toPayload(answer.deserialize()),
					new AsyncCallback() {

						public void onFailure(Throwable caught) {
							main.clear();
							main.add(new Label("ERROR: " +
							                   caught.getMessage()));
							main.setVisible(true);
						}

						public void onSuccess(Object result) {
							PocPayload payload = (PocPayload) result;
							PocQuestion question = new PocQuestion();
							ClientModelUtil.toModel(payload, question);
							//question.serialize(payload);
							currentId = question.getIndex();
							String data = "Question\nIndex:\t" +
							              currentId +
							              "\ntype:\t" +
							              question.getType() +
							              "\ntext:\t" +
							              question.getText() +
							              "\nserverTicks:\t" +
							              question.getServerTicks() +
							              "\nresponses:\t" +
							              convert(question.getResponses()) +
							              "\nreferenced:\t" +
							              question.getReferencedObject();
							textArea.setText(data);
							textBox.setText("");

							// now that we got the question, let's show it.
							main.setVisible(true);
						}
					});
			}
		});

		textArea.setStyleName("textarea");
		textBox.setStyleName("textbox");
		button.setStyleName("button");

		VerticalPanel panel = new VerticalPanel();
		panel.setHeight("500px");
		panel.setWidth("450px");
		panel.add(textArea);
		panel.add(textBox);
		panel.add(button);
		panel.setCellHeight(textArea, "300px");
		panel.setCellHeight(textBox, "30px");
		panel.setCellWidth(textArea, "100%");
		panel.setCellWidth(textBox, "100%");
		panel.setCellHorizontalAlignment(textArea,
		                                 HasHorizontalAlignment.ALIGN_CENTER);
		panel.setCellHorizontalAlignment(textBox,
		                                 HasHorizontalAlignment.ALIGN_CENTER);
		panel.setCellHorizontalAlignment(button,
		                                 HasHorizontalAlignment.ALIGN_CENTER);

		main.add(panel);
		//main.setWidgetPosition(panel, 0, 0);
	}

	private static String convert(String[] responses) {
		String buffer = "-- ";
		if (responses == null) {
			buffer += "null";
		} else {
			for (int i = 0; i < responses.length; i++) {
				String response = responses[i];
				buffer += response + " : ";
			}
		}
		return buffer;
	}
}
